﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormTaiKhoanThem : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormTaiKhoanThem()
        {
            InitializeComponent();
        }

        private void BttLuu_Click(object sender, EventArgs e)
        {
            Create();
        }

        private void BttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void FormTaiKhoanThem_Load(object sender, EventArgs e)
        {
            BttLuu.Enabled = false;
            txtMaTaiKhoan.Focus();
            GetListTaiKhoan();
        }

        private bool KiemTraMaTaiKhoan()
        {
            var t = db.TaiKhoans.Find(Convert.ToInt32(txtMaTaiKhoan.Text.Trim()));
            if (t != null)
            {
                return true;
            }
            return false;
        }
        void GetListTaiKhoan()
        {
            cbbTaiKhanCapTren.DataSource = db.TaiKhoans.ToList();
            cbbTaiKhanCapTren.DisplayMember = "Id";
            cbbTaiKhanCapTren.ValueMember = "Id";

            cbbLoaiTaiKhoan.DataSource = db.LoaiTaiKhoans.ToList();
            cbbTaiKhanCapTren.DisplayMember = "Id";
            cbbTaiKhanCapTren.ValueMember = "TenLoaiTaiKhoan";
        }
        private void Create()
        {
            try
            {
                if (KiemTraMaTaiKhoan() == false)
                {
                    var t = new TaiKhoan();
                    t.Id = txtMaTaiKhoan.Text.Trim();
                    t.TaiKhoanCapTren = cbbTaiKhanCapTren.SelectedValue.ToString();
                    t.LoaiTaiKhoanId = Convert.ToInt32(cbbLoaiTaiKhoan.SelectedValue);
                    t.TenTaiKhoan = txtTenTaiKhoan.Text;
                    t.DienGiai = txtDienGiai.Text;
                    db.TaiKhoans.Add(t);
                    db.SaveChanges();
                    Reset();
                }
                else
                {
                    MessageBox.Show("Mã tài khoản: " + txtMaTaiKhoan.Text.Trim() + " đã tồn tại!");
                    txtMaTaiKhoan.Focus();
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

        private void KiemTraDuLieu()
        {
            if (txtMaTaiKhoan.Text != "" && Convert.ToInt32(cbbTaiKhanCapTren.SelectedValue) != 0 && txtTenTaiKhoan.Text != "" && Convert.ToInt32(cbbLoaiTaiKhoan.SelectedValue)!=0)
            {
                BttLuu.Enabled = true;
            }
            else
            {
                BttLuu.Enabled = false;
            }
        }

        private void Reset()
        {
            txtDienGiai.Text = "";
            txtMaTaiKhoan.Text = "";
            txtTenTaiKhoan.Text = "";
            cbbTaiKhanCapTren.SelectedValue = 0;
            cbbLoaiTaiKhoan.SelectedValue = 0;
            BttLuu.Enabled = false;
        }

        private void txtMaTaiKhoan_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void cbbTaiKhanCapTren_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtTenTaiKhoan_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void cbbLoaiTaiKhoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }
    }
}
